* Table C.2: 
* Generate Robustness to Different Border Cutoffs

	** distance to borders
	global alt_dist 30 35 40 45 55 60
	** Conley cutoff
	local dist 10

************************* Calls *************************

	use "$data/calls_cell_input.dta", clear
	
	drop if year > 2017
	drop if year < 2006  
	egen t = group(year)
		
	gen post = (year > 2007)
		
	** gen interaction term of interest
	gen lang_control_post = lang_control * post
	label var lang_control_post "Lang Control * Post"
		
	local replace replace 
	
	label var lang_control_post "Lang Control * Post"
	label var calls_all_per100C "All calls"
	label var calls_tech_per100C "Tech calls"
	label var calls_others_per100C "Other calls"

	local replace replace
	
	foreach i in $alt_dist {
		foreach var in calls_all_per100C lang_control_post {
			
			cap drop r_`var' 
			
			reghdfe `var' c.share_off_speakers#i.t if cells_`i'km_stateborders == 1  , a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) ///
			residuals(r_`var') cluster(tahsil_id) 
		}
		
		* R2 
		reghdfe calls_all_per100C lang_control_post c.share_off_speakers#i.t if cells_`i'km_stateborders == 1, a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) ///
		cluster(tahsil_id) 
		
		local R2 = round(e(r2), 0.001)
		
		* Conley SE
		label var r_lang_control_post "Non-state official language speakers (%) * Post"
		label var r_calls_all_per100C "All calls"
		
		ols_spatial_HAC r_calls_all_per100C r_lang_control_post if cells_`i'km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
		
		outreg2 using "$results/tables/table_C2_`i'km.tex", ///
		keep(r_lang_control_post) nor2 addtext(R-squared, "`R2'") tex(frag) dec(3) bracket label ///
		nocons nonotes stats(coef se) `replace'
	}

************************* Tech Adoption ************************* 

	use "$data/tech_cell_input.dta", clear
	
	assert year > 1996
	egen t = group(year)
	xtset id t

	** Gen post and interaction var 
	gen post = (year > 2007)
	sum post if year < 2008

	gen lang_control_post = lang_control * post

	label var lang_control_post "Lang Control * Post"
	label var hyv_share "HYV Share"
	
	local replace append
	
	foreach i in $alt_dist {
		foreach var in hyv_share lang_control_post {
			
			cap drop r_`var' 
			
			reghdfe `var' c.share_off_speakers#i.t if cells_`i'km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) ///
			residuals(r_`var') cluster(tahsil_id) 
			
			replace r_`var' = sqrt(pop2001) * r_`var' 
		}
		
		
		* R2 
		reghdfe hyv_share lang_control_post c.share_off_speakers#i.t if cells_`i'km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) ///
		 cluster(tahsil_id)
		 
		 local R2 = round(e(r2), 0.001)
			
		* Conley 
		label var r_lang_control_post "Non-state official language speakers (%) * Post"
		label var r_hyv_share "HYV Share"
		
		ols_spatial_HAC r_hyv_share r_lang_control_post if cells_`i'km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=., ///
		lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
		
		outreg2 using "$results/tables/table_C2_`i'km.tex", ///
		keep(r_lang_control_post) nor2 addtext(R-squared, "`R2'") tex(frag) dec(3) bracket label ///
		nocons nonotes stats(coef se) `replace'
	}

************************* Yield *************************

	use "$data/yield_cell_input.dta", clear
	
	** Gen post and interaction var 
	gen post = (year > 2007)
	egen t = group(year)
	gen lang_control_post = lang_control * post
	
	label var lang_control_post "Lang Control * Post"
	label var total_yield_idt "Yield"

	local replace append 
	
	foreach i in $alt_dist {
	
		preserve
		
		foreach var in total_yield_idt lang_control_post {
		
			cap drop r_`var'
			
			reghdfe `var' c.share_off_speakers#i.t if cells_`i'km_stateborders == 1 [aw=pop2001], ///
			a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) ///
			residuals(r_`var') cluster(tahsil_id) 
			replace r_`var' = sqrt(pop2001) * r_`var'
		}
		
		
		* R2 
		reghdfe total_yield_idt lang_control_post c.share_off_speakers#i.t if cells_`i'km_stateborders == 1 [aw=pop2001], ///
			a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) ///
			cluster(tahsil_id) 
		 
		 local R2 = round(e(r2), 0.001)
		 
		* Conley 
		label var r_lang_control_post "Non-state official language speakers (%) * Post"
		label var r_total_yield_idt "Yield"
		
		local dist 10
		ols_spatial_HAC r_total_yield_idt r_lang_control_post if cells_`i'km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
		
		outreg2 using "$results/tables/table_C2_`i'km.tex", ///
		keep(r_lang_control_post) nor2 addtext(R-squared, "`R2'") tex(frag) dec(3) bracket label ///
		nocons nonotes stats(coef se) `replace'
		
		restore
	}
	